package com.mms.web.controller;

import com.github.pagehelper.PageInfo;
import com.mms.model.AjaxResult;
import com.mms.model.pojo.SysUser;
import com.mms.model.vo.TableData;
import com.mms.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

/**
 * @auth wzy
 * @date 2023/11/21 11:44
 */
@Controller
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/index")
    public String index(){
        return "user";
    }


    @GetMapping("/toAdd")
    public String toAdd(){
        return "user_add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(SysUser sysUser, BindingResult bindingResult){

        // todo:参数验证

        // 保存数据
        int result = sysUserService.save(sysUser);

        if (result<=0){
           return AjaxResult.error();
        }

        return AjaxResult.success();
    }

    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list( SysUser sysUser,@RequestParam("page") Integer pageNum, @RequestParam("limit")Integer pageSize){

        // 获取用户集合
        PageInfo<SysUser> pageInfo = sysUserService.pageList(sysUser, pageNum, pageSize);


        TableData<SysUser> tableData = new TableData<SysUser>(pageInfo.getList(),pageInfo.getTotal());

        return AjaxResult.success(tableData);
    }
}
